$(function () {
  // console.log('hello world!');
  let $btnClose = $('.close'),
    $btnCancel = $('.cancel-btn'),
    $btnSearch = $('.search-btn'),
    $txtContent = $('.text'),
    $titleBar = $('.dlg-titlebar'),
    $showbtn = $('.show-btn'),
    $all = $('.dlg-search');

  $('.dialogbox').draggable({ handle: $titleBar });
  function destory() {
    $('.show-btn').show();
    $all.css({ display: 'none' })
  }

  $showbtn.click(() => {
    // console.log('???');
    $('.show-btn').hide();
    $('.dlg-search').show();
  })
  function verify() {
    if ($txtContent.val() !== '') {
      $btnSearch.removeAttr('disabled');
    } else {
      $btnSearch.attr('disabled', 'disabled');
    }
  }

  function init() {
    $('input[value="up"]').removeAttr('checked');
    $('input[value="down"]')[0].checked = true;
    $('input[type="checkbox"]').removeAttr('checked');
    // $all.css({display:'none'});
    $btnSearch.attr('disabled', 'disabled');
    $txtContent.val('');
    $txtContent.focus();
  }

  function show(searchHandler) {
    init();
    $btnClose.click(destory);
    $btnCancel.click(destory);
    $txtContent.keyup(verify);
    $btnSearch.click(function () {
      searchHandler({
        content: $txtContent.val(),
        capitalSense: $('input[type="checkbox"]:checked').val() === 'capital-sense',
        direction: $('input[name="direction"]:checked').val()
      });
    });
  }
  show()
});
